<?php

namespace Liuhelong\LaravelAdmin\Wechat\Http\Controllers;

use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use Liuhelong\LaravelAdmin\Wechat\Models\WechatOffiaccountMenu;
use Encore\Admin\Tree;
use Encore\Admin\Layout\Content;
use Encore\Admin\Layout\Row;
use Encore\Admin\Layout\Column;
use Request;
use Illuminate\Support\Arr;
use Encore\Admin\Admin;

class WechatOffiaccountMenuController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '公众号菜单';
	public $menus;

	public function __construct(){
		$this->menus = cache('menus');
	}
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    public function index(Content $content)
    {
        /* $grid = new Grid(new WechatOffiaccountMenu());
        return $grid; */
		$tree = new Tree(new WechatOffiaccountMenu);
		$tree->disableCreate();
		$tree->disableRefresh();		
		$tree->branch(function ($branch) {
            
			return $branch['name']??'';
		});
		
		$tree->tools(function (Tree\Tools $tools) {
			$tools->add(new \Liuhelong\LaravelAdmin\Wechat\Actions\WechatOffiaccountMenu\Download());
			$tools->add(new \Liuhelong\LaravelAdmin\Wechat\Actions\WechatOffiaccountMenu\Release());
		});

		$content->row(function(Row $row) use ($tree) {
			$row->column(5,function (Column $column) use ($tree){
				$column->row($tree);
			});
			$row->column(7,function (Column $column){
				// 选填
				$column->row($this->form());
			});
		})->header('公众号菜单');
		
		
		Admin::script("$('[data-toggle=\"popover\"]').popover();");
		
        return $content;
    }
	
	
    public function store(){
		// 列表页排序
		if(Request::post('_order')){
			$orders = json_decode(Request::post('_order'));
			$this->updateOrder($orders);
			
			cache(['menus'=>$this->menus]);
			return;
		}
		
		parent::store();
	}

	
    public function destroy($id){
		unset($this->menus[$id]);
		cache(['menus'=>$this->menus]);
	}
	/**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(WechatOffiaccountMenu::findOrFail($id));



        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new WechatOffiaccountMenu());

		$form->text('name','标题');
		
		$farthers = Arr::where(cache('menus'), function ($value, $key) {
			return !$value['parent_id'];
		});
		$farthers = Arr::pluck($farthers,'name','id');
		
		$form->select('parent_id','上级菜单')->options($farthers);
		
		//$form->number('order','排序值')->default(99);
		
		$form->select('type', '菜单类型')
		->options([
			'view' => '链接',
			'click' => 'click事件',
			'miniprogram' => '跳转小程序',
			'scancode_push' => '扫码推送',
			'scancode_waitmsg' => '扫码等待',
			'pic_sysphoto' => '拍照',
			'pic_photo_or_album' => '拍照/相册',
			'pic_weixin' => '相册',
			'location_select' => '位置',
		])
		->when( 'view', function (Form $form) {
			$form->url('link', __('Link'));
		})->when('miniprogram', function (Form $form) {
			$form->text('appid', __('Appid'));
			$form->text('pagepath', __('Pagepath'));
			$form->url('url', __('Back url'));

		})->when('in', ['click', 'scancode_push', 'scancode_waitmsg', 'pic_sysphoto', 'pic_photo_or_album', 'pic_weixin', 'location_select'], function (Form $form) {
            $form->text('key', __('Key'));
        });
		
		if(!$form->isEditing()){
			$form->setAction(route('wechat-o-menus.store'));
		}
		
		$form->tools(function (Form\Tools $tools) {

			// 去掉`列表`按钮
			$tools->disableList();

			// 去掉`删除`按钮
			$tools->disableDelete();

			// 去掉`查看`按钮
			$tools->disableView();
		});

        return $form;
    }
	
	private function updateOrder($orders,$parent_id = 0){
		foreach($orders as $order=>$data){
			$this->menus[$data->id]['order'] = $order;
			$this->menus[$data->id]['parent_id'] = $parent_id;
			
			if(!empty($data->children)){
				$this->updateOrder($data->children,$data->id);
			}
		}
	}
}
